package com.wswy.wzcx.ui.module.feedback

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import com.che.libcommon.utils.optional.Optional
import com.wswy.wzcx.api.Api
import com.wswy.wzcx.module.base.BaseViewModel
import com.wswy.wzcx.module.base.Resource
import com.wswy.wzcx.module.base.UserViewModel
import com.wswy.wzcx.module.base.onResult
import com.wswy.wzcx.utils.StringUtils

class FeedbackViewModel : UserViewModel() {

    private val feedbacksLiveData = MutableLiveData<Resource< List< FBaseVO >? >>()

    private val commonFeedbackLiveData = MutableLiveData<Resource< List<CommonFeedbackQA>? >>()

    fun commonFeedback(){
        addRun(Api.get().commonFeedbackList().onResult(commonFeedbackLiveData))
    }

    fun myFeedback(){

        feedbacksLiveData.postValue(Resource.loading())

        addRun(Api.get().myFeedbackList().map {
            val list = ArrayList<FBaseVO>()

            it.orNull()?.forEach {

                list.add(FQuestionVO(it.type,StringUtils.formatTime((it.createTime?:0)*1000)?.toString(),it.question,it.images))

                if (it.replyStatus == true){
                    list.add(FReplyVO(StringUtils.formatTime((it.answerTime?:0)*1000)?.toString(),it.answerContent,it.replyImages))
                }else{
                    list.add(FReplyVO(null,"请耐心等待回复",it.replyImages))
                }

            }


            Optional.ofNullable(list)
        }.onResult({

            feedbacksLiveData.postValue(Resource.success(it))

        },{

            feedbacksLiveData.postValue(Resource.Companion.error(it))

        }))
    }


    fun getCommonFeedbackLiveData(): LiveData<Resource<List<CommonFeedbackQA>?>> = commonFeedbackLiveData


    fun getMyFeedbackListLiveData(): LiveData<Resource<List<FBaseVO>?>> =  feedbacksLiveData

}